package com.hgb.mall.admin.services.impl;


import com.hgb.mall.admin.dao.PmsSkuStockDao;
import com.hgb.mall.admin.services.PmsSkuStockService;
import com.hgb.mall.mbg.entity.PmsSkuStock;
import com.hgb.mall.mbg.mapper.PmsSkuStockDynamicSqlSupport;
import com.hgb.mall.mbg.mapper.PmsSkuStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
import static org.mybatis.dynamic.sql.SqlBuilder.isLikeWhenPresent;

/**
 * 商品sku库存管理Service实现类
 * Created by hgb on 2020/4/27.
 */
@Service
public class PmsSkuStockServiceImpl implements PmsSkuStockService {
    @Autowired
    private PmsSkuStockMapper skuStockMapper;
    @Autowired
    private PmsSkuStockDao skuStockDao;

    @Override
    public List<PmsSkuStock> getList(Long pid, String keyword) {

        return skuStockMapper.select(c -> c.where(PmsSkuStockDynamicSqlSupport.productId, isEqualTo(pid))
                                           .and(PmsSkuStockDynamicSqlSupport.skuCode, isLikeWhenPresent(keyword)
                                                   .then(s -> "%" + s + "%")));
    }

    @Override
    public int update(Long pid, List<PmsSkuStock> skuStockList) {
        return skuStockDao.replaceList(skuStockList);
    }
}
